#include <XC8F9463.H>
#include "Function.h"


struct KEY_STRUCT structKey;
struct SWITCH_STRUCT structSwitch[SWITCH_MAX];
/******************************************************************************************************
*  功     能：按键开关检测,10ms执行一次
*  入口参数：无
*  出口参数：structKey.u8KeyValue  返回键值
*  日     期：2023年3月17日
*  作     者：行简
******************************************************************************************************/
#define LONG_SHORT_CLICK       1             //条件编译   1:按键有长按和短按功能    0:按键只有短按功能
#define KEY_MASK               0x01FF        //

#define CHAN_INC_BIT           (1<<0)
#define CHAN_DEC_BIT           (1<<1)
#define POWER_BIT              (1<<2)

#define VOL_INC_BIT            (1<<3)
#define VOL_DEC_BIT            (1<<4)
#define SAVE_CHAN_BIT          (1<<5)  //

#define STEREO_BIT             (1<<6)  //立体声
#define CAMPUS_BIT             (1<<7)  //校园台
#define GET_CHAN_BIT           (1<<8)  //取台

void scan_key(void)
{
  static uint8_t u8step=0;
  switch(u8step)
  {
    case 0://上拉扫描低电平
    /*
      if(P_KEY_CAMPUS)
      {
        structKey.PinNow &=~CAMPUS_BIT;
      }
      else
      {
        structKey.PinNow |=CAMPUS_BIT;
      }

      if(P_KEY_STEREO)
      {
        structKey.PinNow &=~STEREO_BIT;
      }
      else
      {
        structKey.PinNow |=STEREO_BIT;
      }    


      if(P_KEY_GET_CHAN)
      {
        structKey.PinNow &=~GET_CHAN_BIT;
      }
      else
      {
        structKey.PinNow |=GET_CHAN_BIT;
      }
    */
      if(P_KEY_CHAN_DEC)
      {
        structKey.PinNow &=~CHAN_DEC_BIT;
      }
      else
      {
        structKey.PinNow |=CHAN_DEC_BIT;
      }

      if(P_KEY_CHAN_INC)
      {
        structKey.PinNow &=~CHAN_INC_BIT;
      }
      else
      {
        structKey.PinNow |=CHAN_INC_BIT;
      }
      if(P_KEY_POWER)
      {
        structKey.PinNow &=~POWER_BIT;
      }
      else
      {
        structKey.PinNow |=POWER_BIT;
      }

      if(P_KEY_VOL_INC)
      {
        structKey.PinNow &=~VOL_INC_BIT;
      }
      else
      {
        structKey.PinNow |=VOL_INC_BIT;
      }

      if(P_KEY_VOL_DEC)
      {
        structKey.PinNow &=~VOL_DEC_BIT;
      }
      else
      {
        structKey.PinNow |=VOL_DEC_BIT;
      }      
	  //P21,P22,P23,P24,P25����Ϊ��������
	  P2_PU_PD((1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5),0);
	  u8step=1;
	  return;
	break;
    case 1://����ɨ��ߵ�ƽ
      if(P_KEY_SAVE_CHAN==0)
      {
        structKey.PinNow &=~SAVE_CHAN_BIT;
      }
      else
      {
        structKey.PinNow |=SAVE_CHAN_BIT;
      }
	  //P21,P22,P23,P24,P25设置为上拉输入
	  P2_PU_PD((1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5),1);
	  u8step=0;
	break;

    default:
	u8step=0;
	break;
  }
  structKey.PinLast=structKey.PinNow;
 #if LONG_SHORT_CLICK
 //只有长按
  if(((structKey.PinNow&KEY_MASK)==0)||(structKey.PinNow!=structKey.PinLast))
  {
    structKey.count1=0;
    structKey.bitLock=0;
    if(structKey.bitShortClickTmp)
    {
      structKey.bitShortClickTmp=0;
      structKey.bitShortClick=1;
    }
  }
  else if(structKey.bitLock==0)
  {
    structKey.count1++;
    if(structKey.count1==3)
    {
      structKey.bitShortClickTmp=1;
      structKey.KeyValue=structKey.PinNow;
    }
    if(structKey.count1>40)
    {
      structKey.bitLock=1;
      structKey.bitShortClickTmp=0;
      structKey.bitLongClick=1;
      structKey.KeyValue=structKey.PinNow;
    }
  }

#else
  //只有短按没有长按
  if(((structKey.PinNow&KEY_MASK)==0)||(structKey.PinNow!=structKey.PinLast))
  {
    structKey.count1=0;
    structKey.bitLock=0;
  }
  else if(structKey.bitLock==0)
  {
    structKey.count1++;
    if(structKey.count1>25)
    {
      structKey.bitShortClick=1;
      structKey.bitLock=1;
      structKey.KeyValue=structKey.PinNow;
    }
  }
#endif
}
/******************************************************************************************************
*  功     能：按键服务函数
*  入口参数：structKey.bitShortClick  structKey.u8KeyValue  structKey.bitLongClick
*  出口参数：
*  日     期：2023年3月19日
*  作     者：
******************************************************************************************************/
void key_service(void)
{
  static uint8_t u8i=0;
  uint16_t u16tmp=0;

#if  LONG_SHORT_CLICK
  if(structKey.bitLongClick)
  {

    switch(structKey.KeyValue)
    {

      case POWER_BIT: //长按开关机
	  structKey.bitLongClick=0;
	    if((StructRadio.u8status==SLEEP) || (StructRadio.u8status==SLEEP_TIM))
		{
		  StructRadio.u8status=WAKE_UP_TUNE;
		  StructRadio.disStruct.enumDisWorkingStatus=SHOW_NORMAL;
		}
		else
		{
		  StructRadio.u8status=ENTERING_SLLEEP;
		}
      break;

      case CHAN_INC_BIT://开启向上自动搜台
	  structKey.bitLongClick=0;
      #if 1
      switch(StructRadio.u8status)
      {

        case WAKE_UP_TUNE://开机唤醒

        break;

		//case CAMPUS:
        case WORKIING:
		StructRadio.u8ManualSeekEnable=0x01;//选台+
        StructRadio.u8status=MANUAL_SEARCH;
        break;

        case MANUAL_SEARCH://手动搜台

        break;
      }
      #endif
      break;

      case CHAN_DEC_BIT://开启向下自动搜台
	  structKey.bitLongClick=0;
      #if 1
      switch(StructRadio.u8status)
      {

        case WAKE_UP_TUNE://开机唤醒

        break;

        case WORKIING:
		StructRadio.u8ManualSeekEnable=0x02;//选台-
        StructRadio.u8status=MANUAL_SEARCH;
        break;

        case MANUAL_SEARCH://手动搜台

        break;
      }
      #endif
      break;

      case VOL_INC_BIT://音量+
      if(structKey.PinLast!=VOL_INC_BIT)//按键释放
      {
        u8i=0;
	    structKey.bitLongClick=0;
      }
	  else
	  {
        if(u8i)
        {
          u8i--;
          return;
        }
        if(u8i==0)
        {
          u8i=20;
		  goto VOL_INC;
        }
	  }
	  break;

      case VOL_DEC_BIT://音量-
      if(structKey.PinLast!=VOL_DEC_BIT)//按键释放
      {
        u8i=0;
	    structKey.bitLongClick=0;
      }
	  else
	  {
        if(u8i)
        {
          u8i--;
          return;
        }
        if(u8i==0)
        {
          u8i=20;
		  goto VOL_DEC;
        }
	  }
	  break;

      case SAVE_CHAN_BIT://存台
	  structKey.bitLongClick=0;
      if(StructRadio.u8status==WORKIING)
      {
        StructRadio.u8status=CAMPUS;
        StructRadio.disStruct.u8timeOut=5;
      	u16tmp=get_current_channel();
      	write_rda5807_chan_to_eeprom(CAMPUS_STATION_BASE_EEPROM_ADDR+StructRadio.u8CurrentCampusStationSerialNumber*2,u16tmp);
      }
      break;


      case CHAN_DEC_BIT+VOL_DEC_BIT://时间调整组合键
	  structKey.bitLongClick=0;
	  if(StructRadio.u8status==SLEEP_TIM)//只有在sleep tim模式才能调整时间
	  {
	    switch(StructRadio.disStruct.enumDisTimeStatus)
        {
          case SHOW_CLOCK:
          StructRadio.disStruct.u8x=6;
          StructRadio.disStruct.u8timeOut=50;
		  StructRadio.disStruct.enumDisTimeStatus=CHANGE_HOUR;
          break;
        }
	  }
      break;

      default:
      break;
    }
  }
#endif

  if(structKey.bitShortClick)
  {
    structKey.bitShortClick=0;
	switch(structKey.KeyValue)
    {
      case POWER_BIT://电源键程
      switch(StructRadio.u8status)
      {
        case SLEEP_TIM:
        StructRadio.u8status=SLEEP;
        break;

        case SLEEP:
        StructRadio.u8status=SLEEP_TIM;
        break;

		case WORKIING:
		if(StructRadio.disStruct.enumDisWorkingStatus==SHOW_TIME)
		StructRadio.disStruct.enumDisWorkingStatus=SHOW_NORMAL;
		else
		StructRadio.disStruct.enumDisWorkingStatus=SHOW_TIME;
		break;

		case MANUAL_SEARCH:
        StructRadio.u8status=WORKIING;
        StructRadio.disStruct.enumDisWorkingStatus=SHOW_NORMAL;
        //StructRadio.enumAutomaticSeekStep=DETERMINE_WHETHER_TO_START;
		StructRadio.u8ManualSeekStep=0;
        StructRadio.u8ManualSeekEnable=0;
		mute_enable(0);
		//bcr_seek_bit();
		break;
	  }
      break;

      case CHAN_INC_BIT://选台+
	  if(StructRadio.u8status==WORKIING)
      {
        if(StructRadio.disStruct.enumDisWorkingStatus==SHOW_NORMAL)
		{
          if(StructRadio.u16RadioFrequency<1080)
          StructRadio.u16RadioFrequency+=1;
          else
          StructRadio.u16RadioFrequency=500;
          write_frequency_to_rda5807(StructRadio.u16RadioFrequency);
        }
      }
      else if(StructRadio.u8status==MANUAL_SEARCH)
      {
        StructRadio.u8status=WORKIING;
        StructRadio.disStruct.enumDisWorkingStatus=SHOW_NORMAL;
		StructRadio.u8ManualSeekStep=0;
        StructRadio.u8ManualSeekEnable=0;
		#if BK1080
		bcr_seek_bit();
		bcr_tune_bit();
		enter_sleep();
		exit_sleep();
		#endif
		write_frequency_to_rda5807(StructRadio.u16RadioFrequency);
		mute_enable(0);
      }

      break;

      case CHAN_DEC_BIT://选台-
      if((StructRadio.u8status==WORKIING) || (StructRadio.u8status==CAMPUS))
      {
        if((StructRadio.disStruct.enumDisWorkingStatus==SHOW_NORMAL)||(StructRadio.disStruct.enumDisWorkingStatus==SHOW_TIME))
        {
          if(StructRadio.u16RadioFrequency>500)
          StructRadio.u16RadioFrequency-=1;
          else
          StructRadio.u16RadioFrequency=1080;
          write_frequency_to_rda5807(StructRadio.u16RadioFrequency);
        }
      }
      else if(StructRadio.u8status==MANUAL_SEARCH)
      {
        StructRadio.u8status=WORKIING;
        StructRadio.disStruct.enumDisWorkingStatus=SHOW_NORMAL;
        //StructRadio.enumAutomaticSeekStep=DETERMINE_WHETHER_TO_START;
		StructRadio.u8ManualSeekStep=0;
        StructRadio.u8ManualSeekEnable=0;
		#if BK1080
		bcr_seek_bit();
		bcr_tune_bit();
		enter_sleep();
		exit_sleep();
		#endif
		write_frequency_to_rda5807(StructRadio.u16RadioFrequency);
		mute_enable(0);
      }
	  else if(StructRadio.u8status==SLEEP_TIM)
	  {
	    StructRadio.disStruct.u8x=6;
        StructRadio.disStruct.u8timeOut=100;
	    switch(StructRadio.disStruct.enumDisTimeStatus)
        {
          case SHOW_CLOCK:
		  //StructRadio.disStruct.enumDisTimeStatus=CHANGE_HOUR;
          break;
          case CHANGE_HOUR:
		  StructRadio.disStruct.enumDisTimeStatus=CHANGE_MIN;
          break;
          case CHANGE_MIN:
		  StructRadio.disStruct.enumDisTimeStatus=SHOW_CLOCK;
          break;
        }
	  }
      break;

      case SAVE_CHAN_BIT://短按取台
      StructRadio.u8status=WORKIING;
      StructRadio.disStruct.enumDisWorkingStatus=SHOW_NORMAL;
	  StructRadio.u8ManualSeekStep=0;
      StructRadio.u8ManualSeekEnable=0;
      u16tmp=read_rda5807_chan_from_eeprom(CAMPUS_STATION_BASE_EEPROM_ADDR+StructRadio.u8CurrentCampusStationSerialNumber*2);
      write_frequency_to_rda5807(u16tmp);
      mute_enable(0);
      break;

      case VOL_INC_BIT://音量+
VOL_INC:
      if(StructRadio.u8status==MANUAL_SEARCH)
      {
        StructRadio.u8status=WORKIING;
        StructRadio.disStruct.enumDisWorkingStatus=SHOW_NORMAL;
		StructRadio.u8ManualSeekStep=0;
        StructRadio.u8ManualSeekEnable=0;
		#if BK1080
		bcr_seek_bit();
		bcr_tune_bit();
		enter_sleep();
		exit_sleep();
		#endif
		write_frequency_to_rda5807(StructRadio.u16RadioFrequency);
		mute_enable(0);
      }
      switch(StructRadio.u8status)
      {
        case WORKIING:
        if(StructRadio.disStruct.enumDisWorkingStatus==SHOW_NORMAL)
        {
          StructRadio.disStruct.enumDisWorkingStatus=SHOW_VOL;
        }
        if(StructRadio.disStruct.enumDisWorkingStatus==SHOW_TIME)
        {
          StructRadio.disStruct.enumDisWorkingStatus=SHOW_VOL;
        }
        if(StructRadio.u8volumeLevel<15)
        {
          StructRadio.u8volumeLevel++;
          set_radio_volume(StructRadio.u8volumeLevel);
          E2_WR(VOLUME_LEVEL_EEPROM_ADDR,StructRadio.u8volumeLevel);
        }
        StructRadio.disStruct.u8timeOut=20;

        break;

        case SLEEP_TIM:
        switch(StructRadio.disStruct.enumDisTimeStatus)
	    {
          case CHANGE_HOUR://调整小时
          StructRadio.disStruct.u8x=6;
          StructRadio.disStruct.u8timeOut=40;
          if(timerStruct.u8hour<23)
          {
            timerStruct.u8hour++;
          }
          else
          {
            timerStruct.u8hour=0;
          }
          break;

          case CHANGE_MIN://调整分钟
          StructRadio.disStruct.u8x=6;
          StructRadio.disStruct.u8timeOut=40;
          if(timerStruct.u8min<59)
          timerStruct.u8min++;
          else
          timerStruct.u8min=0;
          break;
	    }
        break;
      }

      break;

      case VOL_DEC_BIT://音量-
VOL_DEC:
      if(StructRadio.u8status==MANUAL_SEARCH)
      {
        StructRadio.u8status=WORKIING;
        StructRadio.disStruct.enumDisWorkingStatus=SHOW_NORMAL;
		StructRadio.u8ManualSeekStep=0;
        StructRadio.u8ManualSeekEnable=0;
		#if BK1080
		bcr_seek_bit();
		bcr_tune_bit();
		enter_sleep();
		exit_sleep();
		#endif
		write_frequency_to_rda5807(StructRadio.u16RadioFrequency);
		mute_enable(0);
      }
      switch(StructRadio.u8status)
      {
        case WORKIING:
        if(StructRadio.disStruct.enumDisWorkingStatus==SHOW_NORMAL)
        {
          StructRadio.disStruct.enumDisWorkingStatus=SHOW_VOL;
        }
        if(StructRadio.disStruct.enumDisWorkingStatus==SHOW_TIME)
        {
          StructRadio.disStruct.enumDisWorkingStatus=SHOW_VOL;
        }
        if(StructRadio.u8volumeLevel>0)
        {
          StructRadio.u8volumeLevel--;
          set_radio_volume(StructRadio.u8volumeLevel);
          E2_WR(VOLUME_LEVEL_EEPROM_ADDR,StructRadio.u8volumeLevel);
        }
        StructRadio.disStruct.u8timeOut=20;
        break;

        case SLEEP_TIM:
	    switch(StructRadio.disStruct.enumDisTimeStatus)
	    {
          case CHANGE_HOUR://调整小时
          StructRadio.disStruct.u8x=6;
          StructRadio.disStruct.u8timeOut=100;

          if(timerStruct.u8hour>0)
          {
            timerStruct.u8hour--;
          }
          else
          {
            timerStruct.u8hour=23;
          }
          break;

          case CHANGE_MIN://调整分钟
          StructRadio.disStruct.u8x=6;
          StructRadio.disStruct.u8timeOut=100;
          if(timerStruct.u8min>0)
          timerStruct.u8min--;
          else
          timerStruct.u8min=59;
          break;
	    }
	  }
      break;

	  default:
	  break;
    }
  }


}


uint8_t get_chan_dec_key_status(void)
{
  return (structKey.PinLast==CHAN_DEC_BIT);
}
uint8_t get_chan_inc_key_status(void)
{
  return (structKey.PinLast==CHAN_INC_BIT);
}


/******************************************************************************************************
*  ��     �ܣ��������Ƿ����,�������ر����������
*  ��ڲ�����
*  ���ڲ�����
*  ��     �ڣ�2023��3��19��
*  ��     �ߣ�
******************************************************************************************************/
void scan_switch(void)
{
  if(P_5V!=0)  //高电平有效   
  {
    structSwitch[SWITCH_5V].u8count2=0;
	structSwitch[SWITCH_5V].u8count1++;
	if(structSwitch[SWITCH_5V].u8count1>7)
    structSwitch[SWITCH_5V].bitStatus=1;
  }
  else
  {
    structSwitch[SWITCH_5V].u8count1=0;
    structSwitch[SWITCH_5V].u8count2++;
    if(structSwitch[SWITCH_5V].u8count2>7)
    structSwitch[SWITCH_5V].bitStatus=0;
  }

  if(P_CHRG==0)	 //低电平有效
  {
    structSwitch[SWITCH_CHRG].u8count2=0;
	structSwitch[SWITCH_CHRG].u8count1++;
	if(structSwitch[SWITCH_CHRG].u8count1>7)
    structSwitch[SWITCH_CHRG].bitStatus=1;
  }
  else
  {
    structSwitch[SWITCH_CHRG].u8count1=0;
    structSwitch[SWITCH_CHRG].u8count2++;
    if(structSwitch[SWITCH_CHRG].u8count2>7)
    structSwitch[SWITCH_CHRG].bitStatus=0;
  }
}
